/**
 * 
 */
package com.kingcore.commerce.admin.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.kingcore.commerce.admin.bean.BookBean;
import com.kingcore.commerce.base.dao.CommerceDaoImpl;
import com.kingcore.framework.bean.Pagination;

/**
 * @author Zeven
 *
 */
@Repository("bookDao")
public class BookDaoImpl extends CommerceDaoImpl<BookBean, Long> implements BookDao {
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	} 

	private final static String Sql_insert = "insert into res_book(SN,NAME_CN,NAME_EN,PUBLISH,PUBLISH_DATE,INSERT_DATE,UPDATE_DATE)"
			+ " values(?,?,?,?,?,?,?)";
	@Override
	public BookBean insert(BookBean model, Map<String,Object> param) throws Exception {

		Date now = new Date();
		model.setInsertDate(now);
		model.setUpdateDate(now);
		
		List<Object> params = new ArrayList<Object>();
		params.add( model.getSn() );
		params.add( model.getNameCn() );
		params.add( model.getNameEn() );
		params.add( model.getPublish() );
		params.add( model.getPublishDate() );
		params.add( model.getInsertDate() );
		params.add( model.getUpdateDate() );
		
		Integer key = super.executeUpdate(Sql_insert, params.toArray(), true);
		model.setId( key.longValue() );   
		//super.getl
		return model;
	}

	private final static String Sql_delete = "delete FROM res_book where id=?";
	@Override
	public BookBean delete(Long modelPK, Map<String,Object> param) throws Exception {
		super.executeUpdate(Sql_delete, modelPK);
		return null;
		
	}

	private final static String Sql_update = "update res_book set SN=?,NAME_CN=?,NAME_EN=? where id=?";
	@Override
	public BookBean update(BookBean model, Map<String,Object> param) throws Exception {
		List<Object> params = new ArrayList<Object>();
		params.add( model.getSn() );
		params.add( model.getNameCn() );
		params.add( model.getNameEn() );
		params.add( model.getId() );
		 
		super.executeUpdate(Sql_update, params.toArray() );
		return model;
		
	}
	
	private final static String Sql_selectOne = "SELECT ID,SN,NAME_CN,NAME_EN,PUBLISH,PUBLISH_DATE,INSERT_DATE,UPDATE_DATE FROM res_book where id=?";
	@Override
	public BookBean selectOne(Long modelPK, Map<String,Object> param) throws Exception {
		return (BookBean) super.queryForBean(BookBean.class, Sql_selectOne, modelPK);
	}

	private final static String Sql_selectList = "select a,b from tbl";
	@Override
	public List<BookBean> selectList(BookBean model, Map<String,Object> param) throws Exception {
		return (List<BookBean>) super.queryForList(BookBean.class, Sql_selectList);
	}

	private final static String Sql_selectPagedList_data = "SELECT ID,SN,NAME_CN,NAME_EN,PUBLISH,PUBLISH_DATE,INSERT_DATE,UPDATE_DATE FROM res_book";
	private final static String Sql_selectPagedList_count = "select count(*) FROM res_book";
	@Override
	public Pagination selectPage( BookBean model, int pageNo, int pageSize, Map<String,Object> param) throws Exception {
		List<Object> params = new ArrayList<Object>();
//		params.add( model.getNameEn() );
//		params.add( model.getNameCn() );
//		params.add( model.getId() );
		
		List<BookBean> list = (List<BookBean>) super.queryForPagedList( null, 
											BookBean.class, 
											Sql_selectPagedList_count, 
											Sql_selectPagedList_data, 
											params.toArray() );
		
		return new Pagination(pageNo, pageSize, 1, list);
	}
 
}
